const fs=require("fs");
const http=require("http");

const server=http.createServer((req,res)=>{
    const readFile=fs.createReadStream('data.txt','utf-8');
    res.writeHead(200,{'content-type':'text/plain'});
    readFile.pipe(res);//将可读流的数据通过管道传输到响应对象（可写流）
    readFile.on('error',(err)=>{
        res.writeHead(500,{'content-type':'text/plain'})
        res.end(`错误file:${err.message}`)
    })
})

server.listen(8081,()=>{
    console.log("server is listening http://localhost:8081");
})